热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

C#|Convert。ToInt64(字符串,表单提供者)方法

C#|Convert。ToInt64(字符串,表单提供者)方法

C# | Convert。ToInt64(字符串,表单提供者)方法

原文:https://www . geesforgeks . org/c-sharp-convert-to int 64 string-iformatprovider-method/

此方法用于使用指定的区域性特定格式信息,将数字的指定字符串表示形式转换为等效的 64 位有符号整数。

语法:

public static long ToInt64 (string value, IFormatProvider provider);

参数:


  • 值:是包含要转换的数字的字符串。

  • 提供程序:它是一个提供区域性特定格式信息的对象。

返回值:该方法返回一个十进制数,它相当于中的数字,如果空值,则返回 0(零)。

异常:


  • 格式异常:如果不是由后跟数字序列(0 到 9)的可选符号组成。

  • overoverowexception:如果代表小于最小值或大于最大值的数字。

以下程序说明了转换的使用。ToInt64(字符串,表单提供者)方法:

例 1:

// C# program to demonstrate the
// Convert.ToInt64() Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
    try {
        // creating object of CultureInfo
        CultureInfo cultures = new CultureInfo("en-US");
        // declaring and initializing String array
        string[] values = {"12345", "+12345",
                                   "-12345"};
        // calling get() Method
        Console.Write("Converted long value"
                    + " from a specified string ");
        for (int j = 0; j < values.Length; j++) 
        {
            get(values[j], cultures);
        }
    }
    catch (FormatException e)
    {
        Console.WriteLine("\n");
        Console.Write("Exception Thrown: ");
        Console.Write("{0}", e.GetType(), e.Message);
    }
    catch (OverflowException e)
    {
        Console.WriteLine("\n");
        Console.Write("Exception Thrown: ");
        Console.Write("{0}", e.GetType(), e.Message);
    }
}
// Defining get() method
public static void get(string s,
           CultureInfo cultures)
{
    // converting string to specified long
    long val = Convert.ToInt64(s, cultures);
    // display the converted long value
    Console.Write(" {0}, ", val);
}
}

Output:

Converted long value from a specified string 12345, 12345, -12345,

例 2:格式异常

// C# program to demonstrate the
// Convert.ToInt64() Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
    try {
        // creating object of CultureInfo
        CultureInfo cultures = new CultureInfo("en-US");
        // declaring and initializing String array
        string[] values = {"12345", "+12345",
                                   "-12345"};
        // calling get() Method
        Console.Write("Converted long value"
                    + " of specified strings: ");
        for (int j = 0; j < values.Length; j++) 
        {
            get(values[j], cultures);
        }
        Console.WriteLine("\n");
        string s = "123 456, 789";
        Console.WriteLine("format of s is invalid ");
        // converting string to specified char
        long val = Convert.ToInt64(s, cultures);
        // display the converted char value
        Console.Write(" {0}, ", val);
    }
    catch (FormatException e)
    {
        Console.Write("Exception Thrown: ");
        Console.Write("{0}", e.GetType(), e.Message);
    }
    catch (OverflowException e) 
    {
        Console.Write("Exception Thrown: ");
        Console.Write("{0}", e.GetType(), e.Message);
    }
}
// Defining get() method
public static void get(string s,
           CultureInfo cultures)
{
    // converting string to
    // specified long value
    long val = Convert.ToInt64(s, cultures);
    // display the converted
    // decimal value
    Console.Write(" {0}, ", val);
}
}

Output:

Converted long value of specified strings: 12345, 12345, -12345,
format of s is invalid
Exception Thrown: System.FormatException

例 3: 适用于飞越异常

// C# program to demonstrate the
// Convert.ToInt64() Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
    try {
        // creating object of CultureInfo
        CultureInfo cultures = new CultureInfo("en-US");
        // declaring and initializing String array
        string[] values = {"12345", "+12345",
                                   "-12345"};
        // calling get() Method
        Console.Write("Converted long value "
                 + "of specified strings: ");
        for (int j = 0; j < values.Length; j++)
        {
            get(values[j], cultures);
        }
        Console.WriteLine("\n");
        string s = "-7922816251426433759354395033500000";
        Console.WriteLine("s is less than the MinValue");
        // converting string to specified long
        long val = Convert.ToInt64(s, cultures);
        // display the converted char value
        Console.Write(" {0}, ", val);
    }
    catch (FormatException e)
    {
        Console.Write("Exception Thrown: ");
        Console.Write("{0}", e.GetType(), e.Message);
    }
    catch (OverflowException e) 
    {
        Console.Write("Exception Thrown: ");
        Console.Write("{0}", e.GetType(), e.Message);
    }
}
// Defining get() method
public static void get(string s,
           CultureInfo cultures)
{
    // converting string to
    // specified long value
    long val = Convert.ToInt64(s, cultures);
    // display the converted long value
    Console.Write(" {0}, ", val);
}
}

Output:

Converted long value of specified strings: 12345, 12345, -12345,
s is less than the MinValue
Exception Thrown: System.OverflowException

参考:


  • https://docs . Microsoft . com/en-us/dotnet/API/system . convert . toint 64?view = net framework-4 . 7 . 2 # System _ Convert _ toint 64 _ System _ String _ System _ IFormatProvider _


推荐阅读
author-avatar
展翅翱翔512
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有